<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 
    浅拷贝 
        把父对象的属性，全部拷贝给子对象，也能实现继承;
        对象存在数据成员拥有堆资源但复制过程未实现数据成员拥有堆资源的拷贝
    -->

    <script>
        // 父对象
        let obj = {
            id: 1,
            name: "hy",
            arr: [1, 2, 3]
        };

        // let newObj = obj; // ？浅拷贝

        // newObj["id"] = 2;
        // console.log("newObj", newObj);
        // console.log("obj", obj);

        // 实现浅拷贝
        // 子对象
        let newObj = {};
        for (let key in obj) {
            // console.log(key); // 键名
            // console.log(obj[key]); // 键值
            newObj[key] = obj[key];
        };
        newObj["id"] = 2;
        newObj["name"] = "Jack";
        newObj["arr"][0] = 999;
        console.log("newObj", newObj);
        console.log("obj", obj);

        // ES6  浅拷贝Object.assign(子对象, 父对象)
        let newObj1 = Object.assign({}, obj);
        newObj1["id"] = 3;
        newObj1["name"] = "Mack";

        console.log("newObj1", newObj1);
    </script>

</body>

</html>